package com.mkh.st.common.log.logback.dingtalk;

import cn.hutool.core.util.StrUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Author: xhx
 * Date: 2021/10/16 9:45 上午
 * FileName: DingTalkHelper
 * Description: 
 */
public class DingTalkHelper {
	private static final String dingTalkWebHookDomain = "https://oapi.dingtalk.com/robot/send?access_token=";

	/**
	 * 发送json串消息
	 */
	public static boolean sendTextMsg(String content, String accessToken) {

		return sendTextMsg(content, accessToken, null, null);
	}

	public static boolean sendTextMsg(String content, String accessToken, String keyWords, List<String> atMobiles) {

		Assert.notNull(content, "消息内容不能为空");
		Assert.notNull(accessToken, "accessToken不能为空");

		if (StrUtil.isNotEmpty(keyWords)) {
			content = keyWords.concat(content);
		}
		try {
			OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
			text.setContent(content);
			String tokenURL = dingTalkWebHookDomain.concat(accessToken);
			DingTalkClient client = new DefaultDingTalkClient(tokenURL);
			OapiRobotSendRequest request = new OapiRobotSendRequest();
			request.setMsgtype("text");
			request.setText(text);
			if (!atMobiles.isEmpty()){
				OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
				at.setAtMobiles(atMobiles);
				at.setIsAtAll(false);
			}
			OapiRobotSendResponse response = client.execute(request);
			//System.out.println(JSONObject.toJSONString(response));
		} catch (Exception e) {

		}
		return true;
	}

}
